# -*- coding: utf-8 -*-
# @Time : 2024/1/20 17:48
# @Author : cute
# @Email : Aggressive_cute@126.com
from sanic import Blueprint, json
from views.jd import core, core_v2

# /cute_api/jd_api/slide
jd_api = Blueprint('jd_api', url_prefix='/cute_api/jd_api')

@jd_api.route("/slide_v2", methods=["POST", "GET"])
def jd_slide_v2(request):
    data_dict = None
    if request.method == "GET":
        data_dict = {
            "code": -1,
            "msg": "failed",
            "data": "请使用POST请求传参！"
        }
    else:
        try:
            client_json = request.json
            # print('client_json:', client_json)
            si = client_json.pop('si')
            obj =core_v2.JdSlideV2(**client_json)
            data_dict = {
                "code": 0,
                "msg": "success",
                "data": obj.test(si)
            }
        except Exception as e:
            data_dict = {
                "code": -1,
                "msg": "failed",
                "data": str(e)
            }

    return json(data_dict)

@jd_api.route("/slide", methods=["POST", "GET"])
def jd_slide(request):
    data_dict = None
    if request.method == "GET":
        data_dict = {
            "code": -1,
            "msg": "failed",
            "data": "请使用POST请求传参！"
        }
    else:
        try:
            client_json = request.json
            data_dict = {
                "code": 0,
                "msg": "success",
                "data": core.JdSlide(**client_json).do_jd()
            }
        except Exception as e:
            data_dict = {
                "code": -1,
                "msg": "failed",
                "data": str(e)
            }

    return json(data_dict)


@jd_api.route("/update_algo_4_7_4", methods=["POST"])
def request_algo(request):
    data_dict = None
    if request.method == "GET":
        data_dict = {
            "code": -1,
            "msg": "failed",
            "data": "请使用POST请求传参！"
        }
        return data_dict
    else:
        try:
            client_json = request.json
            data_dict = {
                "code": 0,
                "msg": "success",
                "data": {
                    # "request_algo": core.update_algo_4_7_4(**client_json),
                    "request_algo": core.update_algo_4_7_4_v2(**client_json),
                }
            }
        except Exception as e:
            data_dict = {
                "code": -1,
                "msg": "failed",
                "data": str(e)
            }

        return json(data_dict)



@jd_api.route("/get_fp_4_7_4", methods=["POST"])
def get_fp_4_7_4(request):
    data_dict = {
        "code": 0,
        "msg": "success",
        "data": {
            'fp': core.get_fp_4_7_4()
        }
    }
    return json(data_dict)


@jd_api.route("/get_h5st", methods=["POST"])
def get_h5st(request):
    data_dict = None
    if request.method == "GET":
        data_dict = {
            "code": -1,
            "msg": "failed",
            "data": "请使用POST请求传参！"
        }
        return data_dict
    else:
        try:
            client_json = request.json
            data_dict = {
                "code": 0,
                "msg": "success",
                "data": {
                    "h5st": core.get_h5stv4_7_4(**client_json),
                }
            }
        except Exception as e:
            data_dict = {
                "code": -1,
                "msg": "failed",
                "data": str(e)
            }

        return json(data_dict)

@jd_api.route("/generateAksParams", methods=["POST"])
def generate_aksParams(request):
    data_dict = None
    if request.method == "GET":
        data_dict = {
            "code": -1,
            "msg": "failed",
            "data": "请使用POST请求传参！"
        }
        return data_dict
    else:
        try:
            client_json = request.json
            data_dict = {
                "code": 0,
                "msg": "success",
                "data": {
                    "aksParams": core.parse_encrypt(client_json),
                }
            }
        except Exception as e:
            data_dict = {
                "code": -1,
                "msg": "failed",
                "data": str(e)
            }

        return json(data_dict)
